var length = 0;
var i = 0;
var tab = null;
var time = 15000;
var timer = null;

function setTime(t) {
    console.log('切换秒数'+t)
    time = t * 1000;
}

function run() {
    console.log('run');
    chrome.tabs.query({currentWindow: true}, function(tabs) {
        length = tabs.length;
        tab = tabs[0];
    });

    if (timer == null) {
        console.log('setInterval');
        timer = setInterval(function(){
            chrome.tabs.highlight({ windowId: tab.windowId, tabs: i },function(){});
            i++;
            console.log(i, length);
            if (i >= length) i = 0;
        }, time);
    }

}

function stop() {
    console.log('stop');
    if (timer != null){
        clearInterval(timer);
        timer = null;
    }
}




chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
        console.log("background.js got a message")
        console.log(request);
        console.log(sender);
        console.log(sendResponse);

        if(request.msg == "setTime"){
            setTime(request.time);
        }

        if(request.msg == "run"){
            run();
        }

        if(request.msg == "stop"){
            stop();
        }
    }
);
